package com.tehui.activity.impl;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.tehui.R;
import com.tehui.activity.BaseActivity;
import com.tehui.activity.LoginActivity;
import com.tehui.fragment.BaseFragment;
import com.tehui.fragment.impl.HomeFragment;
import com.tehui.fragment.impl.NineFragment;
import com.tehui.fragment.impl.PersonFragment;
import com.tehui.utils.tools.PrefUtils;
import com.tehui.utils.ui.UiUtils;
import com.tehui.view.OnlyIconItemView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.BindView;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageBottomTabLayout;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

import static com.tehui.app.MyApplication.IS_TAOKE;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.fl)
    FrameLayout fl;
    private ArrayList<BaseFragment> fragments;
    private int                     mIndex;
    private PageBottomTabLayout     tab;
    private boolean                 isLogin;
    private NavigationController    navigationController;

    @Override
    public void initActivityData() {
        super.initActivityData();
        tab = (PageBottomTabLayout) findViewById(R.id.tab);
        initFragment();
        initTab();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.content_main;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIndexSelected(0);
        navigationController.setSelect(0);
    }

    private void initTab() {

        if (IS_TAOKE) {
            navigationController = tab.custom().addItem(newItem(R.drawable.home, R.drawable.home_a))
                    .addItem(newItem(R.drawable.nine, R.drawable.nine_a)).addItem(newItem(R.drawable.user,
                            R.drawable.user_a)).build();
        } else {
            navigationController = tab.custom().addItem(newItem(R.drawable.home1, R.drawable.home1_a))
                    .addItem(newItem(R.drawable.nine, R.drawable.nine_a)).addItem(newItem(R.drawable.user,
                            R.drawable.user_a)).build();
        }

        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                switch (index) {
                    case 0:
                        setIndexSelected(0);
                        //                      StatusBarUtil.setColor(HomeActivity.this, -438935, 0);
                        break;
                    case 1:
                        //                      StatusBarUtil.setColor(HomeActivity.this, -438935, 0);
                        setIndexSelected(1);
                        break;
                    case 2:
                        //                      StatusBarUtil.setColor(HomeActivity.this, -9862160, 0);
                        isLogin = PrefUtils.getBoolean("isLogin", false);
                        if (isLogin) {
                            setIndexSelected(2);
                        } else {
                            startActivityForResult(new Intent(UiUtils.getContext(), LoginActivity.class), 5);
                        }
                        break;
                }
            }

            @Override
            public void onRepeat(int index) {
            }
        });
    }

    private void initFragment() {

        HomeFragment homeFragment = new HomeFragment();
        NineFragment nineFragment = new NineFragment();
        PersonFragment personFragment = new PersonFragment();

        fragments = new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(nineFragment);
        fragments.add(personFragment);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl, personFragment).hide(personFragment);
        ft.add(R.id.fl, homeFragment);
        ft.commit();

        //默认设置为第0个
        setIndexSelected(0);
    }

    public void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        //隐藏
        ft.hide(fragments.get(mIndex));

        //判断是否添加
        if (!fragments.get(index).isAdded()) {
            ft.add(R.id.fl, fragments.get(index)).show(fragments.get(index));
        } else {
            ft.show(fragments.get(index));
        }

        ft.commit();
        //再次赋值
        mIndex = index;
    }

    //创建一个Item
    private BaseTabItem newItem(int drawable, int checkedDrawable) {
        OnlyIconItemView onlyIconItemView = new OnlyIconItemView(this);
        onlyIconItemView.initialize(drawable, checkedDrawable);
        return onlyIconItemView;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0:
                navigationController.setSelect(2);
                setIndexSelected(2);
                break;
            case 1:
                navigationController.setSelect(0);
                setIndexSelected(0);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消所有请求
        OkGo.getInstance().cancelAll();
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                MobclickAgent.onKillProcess(this);
                finish();
                //System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
